﻿using BusinessLayer.VDataEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace TrialManagementTool.Helper
{
    public class APIAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(HttpActionContext actioncontext)
        {
            if (HttpContext.Current.Session["AdminInfo"] as VDataEntity == null && HttpContext.Current.Session["PromoterInfo"] as VDataEntity == null && string.IsNullOrEmpty(UserHelper.GetUserInfoFromCookie()))
            {
                LogHelper.Info(string.Format("Request api:{0} fail, no user info found.", actioncontext.Request.RequestUri));
                HttpContext.Current.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
                //LogHelper.Info(HttpContext.Current.Request.ServerVariables.ToString());
                LogHelper.Info("CookieInfomation:" + HttpContext.Current.Request.ServerVariables.Get("Http_Cookie"));
                IsAuthorized(actioncontext);
                HandleUnauthorizedRequest(actioncontext);
            }
        }

        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            return false;
        }

        protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
        {
            base.HandleUnauthorizedRequest(actionContext);
        }
    }
}